/*
 * Powered By [rapid-framework]
 * Web Site: http://www.rapid-framework.org.cn
 * Google Code: http://code.google.com/p/rapid-framework/
 * Since 2008 - 2012
 */

package com.tech15.pub.action;

import static javacommon.util.extjs.Struts2JsonHelper.outJson;
import static javacommon.util.extjs.Struts2JsonHelper.outXmlJson;

import java.util.List;
import java.util.Map;

import org.apache.struts2.ServletActionContext;

import javacommon.base.BaseStruts4Action;
import javacommon.util.ResInfo;
import cn.org.rapid_framework.page.Page;

import com.opensymphony.xwork2.ModelDriven;
import com.opensymphony.xwork2.Preparable;
import com.tech15.pub.model.Meeting;
import com.tech15.pub.model.PubData;
import com.tech15.pub.service.MeetingManager;
import com.tech15.pub.service.PubDataManager;
import com.tech15.pub.vo.query.MeetingQuery;

/**
 * @author 胡作正 email:zuozhenghu@sina.com
 * @version 1.0
 * @since 1.0
 */


public class MeetingAction extends BaseStruts4Action implements Preparable,ModelDriven{
	//默认多列排序,example: username desc,createTime asc
	protected static final String DEFAULT_SORT_COLUMNS = "hysj asc"; 
	
	//forward paths
	protected static final String LIST_JSP = "/pub/meeting_list.jsp";
	protected static final String MEETING_LIST_JSP = "/pub/meeting_search.jsp";
	protected static final String CREATE_JSP = "/pub/meeting_add.jsp";
	protected static final String EDIT_JSP = "/pub/meeting_edit.jsp";
	protected static final String SHOW_JSP = "/pub/meeting_show.jsp";
	protected static final String PRINT_JSP = "/pub/meeting_print.jsp";
	
	protected static final String CREATE_JSP2 = "/pub/meeting_add.jsp";
	//redirect paths,startWith: !
	protected static final String LIST_ACTION = "!/pub/list.do";
	
	private MeetingManager meetingManager;
	private PubDataManager pubDataManager;
	
	private Meeting meeting;
	Long id = null;
	private String[] items;

	public void prepare() throws Exception {
		if (isNullOrEmptyString(id)) {
			meeting = new Meeting();
		} else {
			meeting = (Meeting)meetingManager.getById(id);
		}
	}
	
	public void setPubDataManager(PubDataManager manager) {
		this.pubDataManager = manager;
	}
	
	public void setMeetingManager(MeetingManager manager) {
		this.meetingManager = manager;
	}	
	
	public Object getModel() {
		return meeting;
	}
	
	public void setId(Long val) {
		this.id = val;
	}

	public void setItems(String[] items) {
		this.items = items;
	}
	
	/** 执行搜索 */
	public String list() {
		MeetingQuery query = newQuery(MeetingQuery.class,DEFAULT_SORT_COLUMNS);
		
		Page page = meetingManager.findPage(query);
		savePage(page,query);
		return LIST_JSP;
	}
	
	public String search() {
		MeetingQuery query = newQuery(MeetingQuery.class,DEFAULT_SORT_COLUMNS);
		Page page = meetingManager.findPage(query);
		savePage(page,query);
		
		List<PubData> lbList = pubDataManager.searchByPid(new Long(16));
		getRequest().setAttribute("lbList", lbList);
		
		return MEETING_LIST_JSP;
	}
	
	public void timeSlice(){
		MeetingQuery query = newQuery(MeetingQuery.class,DEFAULT_SORT_COLUMNS);
		Map map = meetingManager.timeSlice(query);
		outJson(map);
	}
	
	/** 查看对象*/
	public String show() {
		return SHOW_JSP;
	}
	
	public String print() {
		return PRINT_JSP;
	}
	
	/** 进入新增页面*/
	public String create() {
		return CREATE_JSP;
	}
	
	public String schedule() {
		List<PubData> lbList = pubDataManager.searchByPid(new Long(16));
		getRequest().setAttribute("lbList", lbList);
		return CREATE_JSP2;
	}
	
	/** 保存新增对象 */
	public void save() {
		Map<String, Object> resInfo = null;
		try{
			meeting.setYdr(this.getLoginInfo().getUserid());
			String realpath = ServletActionContext.getServletContext().getRealPath(UPLOADPATH);
			meetingManager.saveMeeting(meeting,realpath,getAttachFile(),getAttachFileFileName(),getAttachFileContentType());
			resInfo = ResInfo.getResInfo("",false, false, 2, this.OPERATE_SUCCESS,"");
		}catch(Exception e){
			e.printStackTrace();
			resInfo = ResInfo.getResInfo("",false, false, 1, this.OPERATE_FAILED,"");
		}
		outXmlJson(resInfo);
	}
	
	/**进入更新页面*/
	public String edit() {
		return EDIT_JSP;
	}
	
	/**保存更新对象*/
	public void update() {
		Map<String, Object> resInfo = null;
		try{
			String realpath = ServletActionContext.getServletContext().getRealPath(UPLOADPATH);
			meetingManager.updateMeeting(meeting,realpath,getAttachFile(),getAttachFileFileName(),getAttachFileContentType());
			resInfo = ResInfo.getResInfo("",false, false, 2, this.OPERATE_SUCCESS,"");
		}catch(Exception e){
			e.printStackTrace();
			resInfo = ResInfo.getResInfo("",false, false, 1, this.OPERATE_FAILED,"");
		}
		outXmlJson(resInfo);
	}
	
	/**删除对象*/
	public void delete() {
		Map<String, Object> resInfo = null;
		try {
			meetingManager.removeById(id);
			resInfo = ResInfo.getResInfo("",false, false, 2, this.OPERATE_SUCCESS,"");
		} catch (Exception e) {
			e.printStackTrace();
			resInfo = ResInfo.getResInfo("",false, false, 1,this.OPERATE_FAILED,"");
		}		
		outXmlJson(resInfo);
	}
	
	/**删除对象*/
	public void delByMy() {
		Map<String, Object> resInfo = null;
		try {
			if(getLoginInfo().getUserid().longValue() == this.meeting.getYdr().longValue()){
				meetingManager.removeById(id);
				resInfo = ResInfo.getResInfo("",false, false, 2, this.OPERATE_SUCCESS,"");
			}else{
				resInfo = ResInfo.getResInfo("",false, false, 1,this.OPERATE_FAILED,"");
			}
		} catch (Exception e) {
			e.printStackTrace();
			resInfo = ResInfo.getResInfo("",false, false, 1,this.OPERATE_FAILED,"");
		}		
		outXmlJson(resInfo);
	}
	
	public String myMeeting(){
		Map<String, Object> resInfo = null;
		try {
			MeetingQuery query = newQuery(MeetingQuery.class,DEFAULT_SORT_COLUMNS);
			query.setUserid(getLoginInfo().getUserid());
			Page page=meetingManager.searchMyMeeting(query);
			savePage(page,query);
			List<PubData> lbList = pubDataManager.searchByPid(new Long(16));
			getRequest().setAttribute("lbList", lbList);
			resInfo = ResInfo.getResInfo("",false, false, 2, this.OPERATE_SUCCESS,"");
			setOpt("my");
		} catch (Exception e) {
			e.printStackTrace();
			resInfo = ResInfo.getResInfo("",false, false, 1,this.OPERATE_FAILED,"");
		}		
		outXmlJson(resInfo);
		return MEETING_LIST_JSP;
	}
	
	private String opt;

	public String getOpt() {
		return opt;
	}

	public void setOpt(String opt) {
		this.opt = opt;
	}
	
	
}
